package com.aurora.source.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aurora.source.R;
import com.aurora.source.home.bean.ContactUsDataBean;
import com.aurora.source.util.ErrorImgUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: ww
 * @description:图片或者视频适配器
 * @date: 2018/9/19
 */
public class ImgOrViderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ContactUsDataBean.CompanyBannerDTOListBean> imgFiles = new ArrayList<>();
    private List<ContactUsDataBean.CompanyVideoDTOListBean> videoFiles = new ArrayList<>();
    private static final int TYPE_IMG = 0;
    private static final int TYPE_VIDEO = 1;
    private OnDeleteClickListener onDeleteClickListener;
    private OnItemClickListener onItemClickListener;
    private boolean canDelete = true;//能删除

    public List<ContactUsDataBean.CompanyBannerDTOListBean> getImgFiles() {
        return imgFiles;
    }

    public List<ContactUsDataBean.CompanyVideoDTOListBean> getVideoFiles() {
        return videoFiles;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<ContactUsDataBean.CompanyBannerDTOListBean> imgFiles, List<ContactUsDataBean.CompanyVideoDTOListBean> videoFiles){
        this.imgFiles.clear();
        this.videoFiles.clear();
        this.imgFiles.addAll(imgFiles);
        this.videoFiles.addAll(videoFiles);
        notifyDataSetChanged();
    }

    public ImgOrViderAdapter(Context context) {
        this.context = context;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener onAddImgClickListener) {
        this.onDeleteClickListener = onAddImgClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < imgFiles.size()) {
            return TYPE_IMG;
        } else {
            return TYPE_VIDEO;
        }
    }

    public void setCanDelete(boolean canDelete) {
        this.canDelete = canDelete;
        notifyDataSetChanged();
    }

    public interface OnDeleteClickListener {
        void onDeleteClick(String resId,int type);
    }

    public interface OnItemClickListener {
        void onItemClick(String url,int type,int position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        if (viewType == TYPE_IMG) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_img, parent, false);
            holder = new ImageHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video, parent, false);
            holder = new ImageHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_IMG) { //图片

            final ImageHolder imageHolder = (ImageHolder) holder;
            Glide.with(context).load(imgFiles.get(position).getCompanyBannerPath())
                    .apply(new RequestOptions().placeholder(R.drawable.ic_default).centerCrop().error(ErrorImgUtil.getErrorImgRandom()))
                    .into(imageHolder.ivImg);
            imageHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            if(canDelete){
                imageHolder.ivDelete.setVisibility(View.VISIBLE);
            }else {
                imageHolder.ivDelete.setVisibility(View.GONE);
            }
            imageHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onDeleteClickListener!=null){
                        onDeleteClickListener.onDeleteClick(imgFiles.get(position).getImageId(),TYPE_IMG);
                    }

                }
            });
            imageHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(imgFiles.get(position).getCompanyBannerPath(),TYPE_IMG,position);
                    }
                }
            });

        } else {
            final ImageHolder imageHolder = (ImageHolder) holder;
            Glide.with(context).load(videoFiles.get(position-imgFiles.size()).getVideoImgPath())
                    .apply(new RequestOptions().placeholder(R.drawable.ic_default).centerCrop().error(ErrorImgUtil.getErrorImgRandom()))
                    .into(imageHolder.ivImg);
            if(canDelete){
                imageHolder.ivDelete.setVisibility(View.VISIBLE);
            }else {
                imageHolder.ivDelete.setVisibility(View.GONE);
            }
            imageHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onDeleteClickListener!=null){
                        onDeleteClickListener.onDeleteClick(videoFiles.get(position-imgFiles.size()).getVideoResourceId(),TYPE_VIDEO);
                    }
                }
            });

            imageHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(videoFiles.get(position-imgFiles.size()).getCompanyVideoPath(),TYPE_VIDEO,position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return imgFiles.size() + videoFiles.size();
    }

    public class ImageHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        public ImageHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
